package com.team.abs.service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.team.abs.dao.taskinsertImpl;

@Service
public class uploadServiceImp implements uploadService{

	@Autowired
	private taskinsertImpl task;
	
	@Override
	public String dataTosql(String task_id,byte[] file) throws IOException {
		
		String PATH="D:\\textfile\\";
		File targetFile=new File(PATH);
		if(!targetFile.exists())
			targetFile.mkdirs();
		FileOutputStream out=null;
		String filename=task_id+".txt";
		 //System.out.println("记入");
		try {
			out=new FileOutputStream(PATH+filename);
			out.write(file);
			out.flush();
		
		}catch(IOException e) {e.printStackTrace();}
		finally {out.close();
		jieba(PATH,task_id);
		plot(PATH,task_id);
		cloud(PATH,task_id);
		}
		
		return "ok";
	}
	public void jieba(String path,String filename) {
		
		 String a=path+filename+".txt";
		String b=path+filename+"_jieba.txt";
	
			try {
				
		    String[] args = new String[] { "python", "D:\\pythonplace\\spider\\weibo\\clean.py", a, b };
		    Process proc = Runtime.getRuntime().exec(args);// 执行py文件
		 
		    BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
		    String line = null;
		   
		    while ((line = in.readLine()) != null) {
		       // System.out.println(line);
		    }
		    in.close();
		    proc.waitFor();
		} catch (IOException e) {
		    e.printStackTrace();
		} catch (InterruptedException e) {
		    e.printStackTrace();
		}
			return ;
	}
	public void plot(String path,String filename) {
		 String a=path+filename+".txt";
		 String b=path+filename+"_plot.txt";

				try {

			    String[] args = new String[] { "python", "D:\\pythonplace\\spider\\weibo\\plot.py", a, b };
			    Process proc = Runtime.getRuntime().exec(args);// 执行py文件
			 
			    BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(),"GB2312"));
			    String line = null;
			   List<String>list=new ArrayList();
			    while ((line = in.readLine()) != null) {
			    	list.add(new String(line.getBytes("UTF-8"),"UTF-8"));
			    }
			    task.insert_task_id_res(Integer.valueOf(filename), list);
			    in.close();
			    proc.waitFor();
			} catch (IOException e) {
			    e.printStackTrace();
			} catch (InterruptedException e) {
			    e.printStackTrace();
			}
				return ;
		
	}
	public void cloud(String path,String filename) {
		 String a=path+filename+".txt";
		 String b=path+filename+".png";
			try { 
			    String[] args = new String[] { "python", "D:\\pythonplace\\spider\\weibo\\cloud.py", a, b };
			    Process proc = Runtime.getRuntime().exec(args);// 执行py文件
			 
			    BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(),"GB2312"));
			    String line = null;
			   
			    while ((line = in.readLine()) != null) {
			        //System.out.println(new String(line.getBytes("UTF-8"),"UTF-8"));
			    }
			    in.close();
			    proc.waitFor();
			} catch (IOException e) {
			    e.printStackTrace();
			} catch (InterruptedException e) {
			    e.printStackTrace();
			}
		
	}
}
